import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as Icons from '@element-plus/icons-vue' // 引入所有图标，并命名为 Icons
import axios from 'axios'
import VueAxios from 'vue-axios'
import * as qiniu from 'qiniu-js';
import echarts from 'echarts'

const app = createApp(App)
// 通过遍历的方式注册所有 svg组件，会牺牲一点点性能
for (let i in Icons) {
  app.component(i, Icons[i])
}

//解决缩放网页报错
const debounce = (fn, delay) => {
  let timer = null;
  return function () {
    let context = this;
    let args = arguments;
    clearTimeout(timer);
    timer = setTimeout(function () {
      fn.apply(context, args);
    }, delay);
  }
}

const _ResizeObserver = window.ResizeObserver;
window.ResizeObserver = class ResizeObserver extends _ResizeObserver {
  constructor(callback) {
    callback = debounce(callback, 16);
    super(callback);
  }
}
// 在请求头中加token    
// 请求拦截器,在请求发出去之前做点什么事
axios.interceptors.request.use(
  config => {
    // 判断是否存在token，如果存在的话，则每个http header都加上token                            
    let token = sessionStorage.getItem('token')
    if (!config.headers.hasOwnProperty('token') && token) {
      config.headers.Authorization = "Bearer " + token;// 必须加上Bearer 后端需要这种格式    
    }
    return config;
  },
  error => {
    _this.$router.push("Login");
    return Promise.reject(error);
  });


app.use(store).use(router).use(ElementPlus).use(qiniu).use(VueAxios, axios).mount('#app')
